package com.niodata.dp.monitor.sharelib.service;

import com.niodata.dp.monitor.sharelib.entity.base.BaseResult;
import com.niodata.dp.monitor.sharelib.entity.file.DataSetCompleteness;
import com.niodata.dp.monitor.sharelib.entity.file.DataSetCompletenessRate;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component("completenessService")
public class DataSetCompletenessService {

  @Autowired
  private RestTemplate restTemplate;

  @Value("${monitor.server.address}")
  private String server;
  @Value("${monitor.server.port}")
  private String port;

  //数据完整性记录查询
  public BaseResult<DataSetCompleteness> getLatestByDatasetId(String datasetId) {
    String execUrl = String.format(
          "http://%s:%s/monitor/file/completeness/latest?datasetId=%s", server, port, datasetId);
    return restTemplate.exchange(execUrl, HttpMethod.GET, null,
          new ParameterizedTypeReference<BaseResult<DataSetCompleteness>>() {
          }).getBody();
  }

  //查询多个数据集的数据完整度
  public BaseResult<DataSetCompletenessRate> getRatesByDataSetIds(List<String> dataSetIds) {
    String queryParam = "";
    if (dataSetIds != null && dataSetIds.size() > 0) {
      queryParam = String.join(",", dataSetIds);
    }
    String execUrl = String.format(
          "http://%s:%s/monitor/file/completeness/rates?dataSetIds=%s", server, port, queryParam);
    return restTemplate.exchange(execUrl, HttpMethod.GET, null,
          new ParameterizedTypeReference<BaseResult<DataSetCompletenessRate>>() {
          }).getBody();
  }

  public BaseResult checkCompletenessOnce(String datasetId) {
    String execUrl = String
          .format("http://%s:%s/monitor/file/completeness/check/once?datasetId=%s", server, port,
                datasetId);
    ResponseEntity<BaseResult> responseEntity = restTemplate
          .postForEntity(execUrl, null, BaseResult.class);
    BaseResult result = responseEntity.getBody();
    return result;
  }

  public BaseResult startCompletenessCheckSchedule(String dataSetId) {
    String execUrl = String
          .format("http://%s:%s/monitor/file/completeness/check/schedule/start?datasetId=%s",
                server,
                port, dataSetId);
    ResponseEntity<BaseResult> responseEntity = restTemplate
          .postForEntity(execUrl, null, BaseResult.class);
    BaseResult result = responseEntity.getBody();
    return result;
  }

  public BaseResult stopCompletenessCheckSchedule(String dataSetId) {
    String execUrl = String
          .format("http://%s:%s/monitor/file/completeness/check/schedule/stop?datasetId=%s", server,
                port, dataSetId);
    ResponseEntity<BaseResult> responseEntity = restTemplate
          .postForEntity(execUrl, null, BaseResult.class);
    BaseResult result = responseEntity.getBody();
    return result;
  }
}
